package com.wh.swagger_validator.controller;

import com.wh.swagger_validator.group.Login;
import com.wh.swagger_validator.pojo.User;
import com.wh.swagger_validator.result.RespResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Length;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @Auther: HuiEr
 * @Date: 2020/2/24 20:25
 * @Description
 */
@RestController
@Validated
@Api(value = "用户控制器",tags = "用户控制器")
public class UserController {


    @RequestMapping(value = "/test1",method = RequestMethod.GET)
    @ApiOperation(value = "这里是test1，测试无参数请求")
    public RespResult<String> test1(){
        RespResult<String> result = new RespResult<>();
        result.setCode(0);
        result.setMsg("无参数请求成功");
        result.setData("success");

        return result;
    }

    @RequestMapping(value = "/test2",method = RequestMethod.GET)
    @ApiOperation(value = "这是测试只有一个username请求数据的接口")
    @ApiImplicitParam(name = "username",value = "用户名",required = true,paramType = "query",dataType = "String" )
    public RespResult<String> test2(@RequestParam("username") String username){
        RespResult<String> stringRespResult = new RespResult<>();
        stringRespResult.setCode(0);
        stringRespResult.setMsg("请求参数为username的请求成功");
        stringRespResult.setData(username);

        return stringRespResult;
    }

    @RequestMapping(value = "/test3",method = RequestMethod.GET)
    @ApiOperation("这是用来测试两个参数请求的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "password",value = "密码",required = true,dataType = "String",paramType = "query")
    })
    public RespResult<String> test3(@RequestParam("username") String username,@RequestParam("password") String password){
        RespResult<String> stringRespResult = new RespResult<>();
        stringRespResult.setCode(0);
        stringRespResult.setMsg("两个参数username和password的请求成功");
        stringRespResult.setData(username+password);

        return stringRespResult;
    }

    @RequestMapping(value = "test4",method = RequestMethod.POST)
    @ApiOperation(value = "这是用来测试对象和请求头的接口",notes = "这里是注意事项")
    @ApiImplicitParam(name = "token",value = "登录令牌",required = true,paramType = "header",dataType = "String")
    public RespResult<User> test4(@RequestBody User user, HttpServletRequest request){
        String token = request.getHeader("token");
        RespResult<User> stringRespResult = new RespResult<>();
        stringRespResult.setCode(0);
        stringRespResult.setMsg("请求参数为对象并且请求头有内容的请求成功"+token);
        stringRespResult.setData(user);

        return stringRespResult;
    }
    @RequestMapping(value = "/test5",method = RequestMethod.POST)
    @ApiOperation(value = "这里是test5，测试文件上传请求")
    public RespResult<String> test5(@RequestParam("file")MultipartFile file){
        RespResult<String> result = new RespResult<>();
        try {
            file.transferTo(new File("E:/bobo.jpg"));
            result.setCode(0);
            result.setMsg("数请求成功");
            result.setData("success");
        } catch (IOException e) {
            result.setCode(-1);
            result.setMsg("数请求失败");
            result.setData("failure");
        }


        return result;
    }

    /*当已经有group了，就要用@Validated，如果你用@Valid是校验不了的*/
    @RequestMapping(value = "/test6",method = RequestMethod.POST)
    @ApiOperation(value = "这里是test6，是测试校验对象接口")
    public String test6(@Valid User user, BindingResult result){
        if (result.hasFieldErrors()){
            List<FieldError> fieldErrors = result.getFieldErrors();
            for (FieldError f: fieldErrors){
                String field = f.getField();
                String defaultMessage = f.getDefaultMessage();
                System.out.println("字段时："+field+"报错信息是；"+defaultMessage);
            }
        }
        return "处理请求中。。。。。。";
    }

    @RequestMapping(value = "/test7",method = RequestMethod.POST)
    @ApiOperation(value = "这里是test7，是测试校验对象接口")
    public String test7(@Validated(Login.class) User user, BindingResult result){
        if (result.hasFieldErrors()){
            List<FieldError> fieldErrors = result.getFieldErrors();
            for (FieldError f: fieldErrors){
                String field = f.getField();
                String defaultMessage = f.getDefaultMessage();
                System.out.println("字段时："+field+"报错信息是；"+defaultMessage);
            }
        }
        return "处理请求中。。。。。。";
    }

    /*单个参数就没有BindingResult，直接抛出异常
    * javax.validation.ConstraintViolationException: test8.username: bbbbb
    * */
    @RequestMapping(value = "/test8",method = RequestMethod.GET)
    @ApiOperation(value = "这是用来校验多个参数的接口测试")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "password",value = "密码",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "mail",value = "邮箱",required = true,paramType = "query",dataType = "String")
    }
    )
    public String test8(@NotNull(message = "not nuull") @Length(message = "bbbbb",min=3,max = 5) @RequestParam("username") String username, @NotNull(message = "not nuull") @RequestParam("password") String password, @NotNull(message = "not nuull")@RequestParam("mail") String mail){

        return "处理请求中。。。。";
    }
}
